package com.kasax.httpclient.interceptors;

import com.kasax.httpclient.beans.Request;
import com.kasax.httpclient.beans.Response;
import com.kasax.httpclient.core.HttpCondec;
import com.kasax.httpclient.core.inters.Interceptor;
import com.kasax.httpclient.utils.LogUtils;

import java.io.IOException;
import java.util.Map;


/**
 * 桥接拦截器
 */
public class BridgeInterceptor implements Interceptor {



    @Override
    public Response intercpt(Chain chain) throws IOException {
        LogUtils.i(getClass(),"intercpt");
        Request request = chain.call().request();
        Map<String, String> headers = request.headers();
        headers.put(HttpCondec.HEAD_HOST, request.url().getHost());
        headers.put(HttpCondec.HEAD_CONNECTION, HttpCondec.HEAD_VALUE_KEEP_ALIVE);
        if (null != request.body()) {
            String contentType = request.body().contentType();
            if (contentType != null) {
                headers.put(HttpCondec.HEAD_CONTENT_TYPE, contentType);
            }
            long contentLength = request.body().contentLength();
            if (contentLength != -1) {
                headers.put(HttpCondec.HEAD_CONTENT_LENGTH, Long.toString(contentLength));
            }
        }
        return chain.procced();
    }
}
